package com.learn.security.utils.message_digest;

import com.learn.security.utils.BaseUtil;
import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD加密工具类
 */
public class MDUtil extends BaseUtil {


    public static final String KEY_MD5 = "MD5";

    /**
     * MD5加密
     *
     * @param data 加密文件
     *
     * @return 加密的字节
     */
    public static byte[] encryptMD5(byte[] data) {
        return encryptSHAAndMd(data, KEY_MD5);
    }

    /**
     * MD5加密
     *
     * @param data 加密文件
     *
     * @return 加密的字符串
     */
    public static String encryptMD52String(byte[] data) {
        byte[] bytes = encryptSHAAndMd(data, KEY_MD5);
        if (bytes == null){
            return null;
        }
        return Hex.encodeHexString(bytes);
    }
}
